<?php
class ProfilesController extends AppController {

	var $name = 'Profiles';
	var $helpers = array('Html', 'Form', 'Javascript', 'Ajax', 'GoogleMap', 'Time', 'Utility');
	var $uses = array('Profile', 'Authake.User');
	var $paginate = array(
		'fields' => array('Profile.slug', 'Profile.realname', 'Profile.latitude', 'Profile.longitude', 'Profile.avetar', 'Profile.dir'),
		'limit' => 8,        
		'order' => array(
			'Profile.slug' => 'asc'
		)
	);
	
	/*var $cacheAction = array(
	    'index'  => 3600,
	'view' => 3600,
	);*/
	
	function index() {
		$this->Profile->recursive = -1;
		$this->set('profiles', $this->paginate());
	}

	
	function _get_profile() {
		if (!$this->Authake->getUserId()) {
			$this->Session->setFlash(__('Invalid User', true), 'error');
			$this->redirect(array('action'=>'index'));
		}
		$user = $this->User->read(null, $this->Authake->getUserId());
		//debug($user);
		$profile = $this->Profile->findByUserId($user['User']['id']);
		//debug($profile);
		if(empty($profile)) {
			$this->Profile->create();
			//$this->data['Profile']['id'] = null;
			$this->data['Profile']['user_id'] = $user['User']['id'];
			$this->data['Profile']['slug'] = $user['User']['login'];
			$this->data['Profile']['latitude'] = Configure::read('Map.latitude');
			$this->data['Profile']['longitude'] = Configure::read('Map.longitude');
			$this->data['Profile']['location'] = Configure::read('Profile.location');
			
			if ($this->Profile->save($this->data)) {
				$this->Session->setFlash(__('Your profile has been added. Please add some personal information!', true));
				//$this->redirect(array('action'=>'home'));
				$id = $this->Profile->getLastInsertID();
			} else {
				$this->Session->setFlash(__('There was an error creating a profile for your user. Please contact the admin!', true));
				$this->log('Error: creating profile for user: ' . $user['User']['login']); 
				//$this->redirect('/');
			}
		}
		return $profile;
	}
	
	function edit($id = null) {
		if (!empty($this->data)) {
			if( !preg_match("/http:\/\//i", $this->data['Profile']['url']) and !empty($this->data['Profile']['url']) ) {
				$this->data['Profile']['url'] = "http://" . $this->data['Profile']['url'];
			}
			if ($this->Profile->save($this->data)) {
				$this->Session->setFlash(__('The Profile has been saved', true));
				$this->redirect(array('action'=>'home'));
			} else {
				$this->Session->setFlash(__('The Profile could not be saved. Please, try again.', true));
			}
		} else {
			$this->data = $this->_get_profile();
			//$this->set('profile', $profile);
		}

	}
	
	function editmap($id = null) {
		if (!empty($this->data)) {
			if ($this->Profile->save($this->data)) {
				$this->Session->setFlash(__('The Profile has been saved', true));
				$this->redirect(array('action'=>'home'));
			} else {
				$this->Session->setFlash(__('The Profile could not be saved. Please, try again.', true));
			}
		} else {
			$this->data = $this->_get_profile();
			//$this->set('profile', $profile);
		}

	}

	function home() {
		$profile = $this->_get_profile();
		//debug($profile);
		$this->set('can_edit', true);
		$this->set('profile', $profile);
		$this->render('view');
	}
	
	function view($slug_id = null) {
		if ($slug_id == null) {
			$this->Session->setFlash(__('Invalid id for Profile', true));
			$this->redirect(array('action'=>'index'));
		}
		if(is_numeric($slug_id)) {
			$profile = $this->Profile->read(null, $slug_id);
		} else {
			$profile = $this->Profile->findBySlug($slug_id);
		}

		/*if(!$this->is_admin() and !$article['Article']['published']) {
			$this->Session->setFlash(__('You don\'t have access to this article', true));
			$this->redirect(array('action'=>'index'));
		}*/

		$this->pageTitle = $profile['Profile']['slug'] . ' / ' . __('Baker profile', true);

		$this->set('profile', $profile);
	}


	function admin_index() {
		$this->Profile->recursive = 0;
		$this->paginate['fields'] = null;
		$this->set('profiles', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Profile.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('profile', $this->Profile->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Profile->create();
			if ($this->Profile->save($this->data)) {
				$this->Session->setFlash(__('The Profile has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Profile could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Profile->User->find('list');
		$this->set(compact('users'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Profile', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Profile->save($this->data)) {
				$this->Session->setFlash(__('The Profile has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Profile could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Profile->read(null, $id);
		}
		$users = $this->Profile->User->find('list');
		$this->set(compact('users'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Profile', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Profile->del($id)) {
			$this->Session->setFlash(__('Profile deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>